<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="ID" placeholder="账号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" >
			</el-table-column>
			<el-table-column prop="username" label="账号" width="200">
			</el-table-column>
			<el-table-column prop="creator" label="创建账户" min-width="200">
			</el-table-column>
			<el-table-column prop="group" label="分组名称" width="200">
			</el-table-column>
			<el-table-column label="操作" min-width="250">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<!--<el-col :span="24" class="toolbar">-->
			<!--<el-pagination layout="sizes, total, prev, pager, next" :page-sizes="[15, 30, 50]" @current-change="handleCurrentChange" @size-change="handleSizeChange" :page-size="pageSize" :total="total" style="float:right;">-->
			<!--</el-pagination>-->
		<!--</el-col>-->

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="账号" >
					<el-input v-model="editForm.username" auto-complete="off" disabled></el-input>
				</el-form-item>
				<el-form-item label="权限类型" >
					<el-select v-model="editValue" placeholder="请选择">
						<el-option
								v-for="item in options"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="新密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off" type="password"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit(editForm.username)" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm"  label-width="120px" :rules="addFormRules" ref="addForm">
				<el-form-item label="账号名称" prop="username">
					<el-input v-model="addForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="addForm.password"  type="password"></el-input>
				</el-form-item>
				<el-form-item label="选择权限分组" prop="power">
					<el-select v-model="value" placeholder="请选择">
						<el-option
								v-for="item in options"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit(addForm.username)" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
                options: [],
                value: '',
                editValue:'',
                ID:'',
				users: [],
				total: 0,
				page: 1,
                pageSize:15,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
                editPwdVisible:false,
				editFormRules: {
                    username: [
                        { required: true, message: '请输入账号', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    power:[
                        { required: true, message: '请选择权限分组', trigger: 'blur' }
                    ]
				},
				//编辑界面数据
				editForm: {

				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    username: [
						{ required: true, message: '请输入账号', trigger: 'blur' }
					],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    power:[
                        { required: true, message: '请选择权限分组', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {

				}
			}
		},
		methods: {
            handleSizeChange(val) {
                this.pageSize = val
                this.getUsers(this.pageSize);
            },
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers(this.pageSize);
			},
            setPwd(){
                this.editPwdVisible = true;
			},
			//获取用户列表
			getUsers() {
				this.listLoading = true;
				this.$axios.get('/admins',{},this.global().token).then((res)=>{
				    if (res.status == 200){
				        this.users = res.data
						this.listLoading = false
					}
				})
                this.$axios.get('/groups',{},this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.options = res.data
						this.value = res.data[0].id
                    }
                })
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
				this.editValue = row.group
				console.log(this.options)
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
                    username:'',
					password:'',
                    group:'',
                    enable:true
				};
			},
			//编辑
			editSubmit: function (username) {
                this.editForm.group = this.editValue
                this.$axios.patch('/admin/'+username,this.editForm,this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.$message({message: '修改成功', type: 'success'});
                        this.editFormVisible = false;
                        this.getUsers();
                    }else{
                        this.$message({message: res.data, type: 'error'});
                    }
                })
			},
			//新增
			addSubmit: function () {
			    this.addForm.group = this.value
                this.$axios.post('/admin',this.addForm,this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.$message({message: '添加成功', type: 'success'});
                        this.addFormVisible = false;
                        this.getUsers();
                    }else{
                        this.$message({message: res.data, type: 'error'});
					}
                })
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>